<?php
// +-------------------------------------------------------------------
// |
// +-------------------------------------------------------------------
// | Copyright (c) 2009-2016 All rights reserved.
// +-------------------------------------------------------------------
namespace Service\Api\V1;

class TaskList
{
    public $login = false; //是否需要登录才能访问该接口
    public $title = "任务列表";
    public $group = '任务';
    public $desc = "";

    public $input = [

    ];

    public $output = [
        'list' => [
            [
                'id' => 'label=任务id;',
                'taskname' => 'label=任务名称;',
                'taskprice' => 'label=任务奖励;',
                'get_status' => 'label=当日任务状态;comment=1已完成0未完成',
            ],
        ],
        'user_info' => [
            'sign_continue_num' =>'label=连续签到次数',
            'sign_total_num' =>'label=累计签到次数',
            'eyes_money' =>'label=护眼鲸币数',
        ],
        'today_sign' => [
            'id' => 'label=任务id;',
            'taskname' => 'label=任务名称;',
            'taskprice' => 'label=任务奖励;',
            'get_status' => 'label=当日签到状态;comment=1已完成0未完成',
        ]
    ];

    public function run($param, $uid, $login_info)
    {
        $list = (array)M('eyes_task')->where(['id' => ['NEQ', 1]])->order('px desc')->select();
        foreach($list as &$item){
            $flag = (array)M('eyes_task_log')->field('id, title')->where([
                'taskid' => $item['id'],
                'record_date' => date('Y-m-d'),
            ])->find();
            if(isset($flag['id'])){
                $item['get_status'] = 1;
            }else{
                $item['get_status'] = 0;
            }
        }
        $user_info['eyes_money'] = $login_info['eyes_money'];
        $flag = M('eyes_task_log')->where(['taskid' => 1, 'userid' => $uid])->count();
        $user_info['sign_total_num'] = $flag?$flag:0;
        $sign_continue_num = 0;
        $start_time = date('Y-m-d');
        while(true){
            $flag = M('eyes_task_log')->where([
                'taskid' => 1,
                'userid' => $uid,
                'record_date' => $start_time
            ])->find();
            if(isset($flag['id'])){
                $sign_continue_num++;
                $start_time = date('Y-m-d', strtotime($start_time . " 12:00:00")-86400);
            }else{
                break;
            }
        }

        $user_info['sign_continue_num'] = $sign_continue_num;

        $today_sign = M('eyes_task')->where(['id' => 1])
            ->find();
        $flag = M('eyes_task_log')->where([
            'taskid' => 1,
            'record_date' => date('Y-m-d'),
            'userid' => $uid,
        ])->find();
        if(isset($flag['id'])){
            $today_sign['get_status'] = 1;
        }else{
            $today_sign['get_status'] = 0;
        }

        return ['list' => $list, 'user_info' =>$user_info, 'today_sign' => $today_sign];
    }
}
